<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.TmppurDAO"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="jxl.Workbook" %>  
<%@page import="jxl.format.Alignment" %> 
<%@page import="jxl.format.Border" %> 
<%@page import="jxl.format.BorderLineStyle" %>  
<%@page import="jxl.format.Colour" %>  
<%@page import="jxl.format.VerticalAlignment" %>  
<%@page import="jxl.write.Label" %>  
<%@page import="jxl.write.WritableCellFormat" %>  
<%@page import="jxl.write.WritableFont" %>  
<%@page import="jxl.write.WritableSheet" %>
<%@page import="jxl.write.WritableWorkbook" %> 
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileOutputStream" %>  
<%@page import="java.io.OutputStream" %> 
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	TmppurDAO tmppurDao = (TmppurDAO)ctx.getBean("TmppurDAO");
	
	Integer teamId = (Integer)session.getAttribute("tid");
	
	String pnStr = request.getParameter("pn");
	String code = request.getParameter("code");
	String codeType = request.getParameter("codeType");
	String date = request.getParameter("date");
	String supplier = request.getParameter("supplier");
	String execl="";
	int pn = 0;
	int pageSize = 10;
	
	if(pnStr != null && !"".equals(pnStr.trim())){
		try{
			pn = Integer.parseInt(pnStr);
		}catch(Exception e){
			pn = 0;
		}
	}
	
	List where = new ArrayList();
	
	where.add(new Object[]{"eq", "state", 1});
	where.add(new Object[]{"eq", "teamId", teamId});
	
	if(code != null && !"".equals(code)){
		if("1".equals(codeType))
			where.add(new Object[]{"like", "tmpCode", code});
		else if("2".equals(codeType))
			where.add(new Object[]{"like", "pr.code", code});
	}
	
	if(date != null && !"".equals(date)){
		where.add(new Object[]{"eq", "date", new SimpleDateFormat("yyyy-MM-dd").parse(date)});
	}
	/*
	if(supplier != null && !"".equals(supplier)){
		where.add(new Object[]{"eq", "date", new SimpleDateFormat("yyyy-MM-dd").parse(date)});
	}
	
	Map map = tmppurDao.getTmppur(pn,pageSize,
			new String[][]{new String[]{"desc","date"}},
			where,
			new String[][]{new String[]{"","id"},new String[]{"","tmpCode"},new String[]{"","pr.code"},new String[]{"","pr.name"},new String[]{"","ru.name"},new String[]{"","pu.reqDate"},new String[]{"","date"}}
			,new String[]{"purreq","project","reqUser"});
			
	*/
	if(supplier != null && !"".equals(supplier)){
		where.add(new Object[]{"like", "su.name", "%" + supplier + "%"});
	}
	
	Map map = tmppurDao.getTmppur(pn,pageSize,
			new String[][]{new String[]{"desc","date"}},
			where,
			new String[][]{new String[]{"","id"},new String[]{"","tmpCode"},new String[]{"","pr.code"},new String[]{"","pr.name"},new String[]{"","ru.name"},new String[]{"","su.name"},new String[]{"","pu.reqDate"},new String[]{"","date"}}
			,new String[]{"purreq","project","reqUser","supplier"});
	
	request.setAttribute("map", map);
	execl = getExecl(request,map);
	int pageTotal = ((Integer)map.get("pageTotal")).intValue();
	int pageNo = ((Integer)map.get("pageNo")).intValue();
	List pageList=getPage(pageTotal,pageNo);
	request.setAttribute("pages",pageList);
	request.setAttribute("xlsName",execl);
	request.setAttribute("xls",true);
	request.setAttribute("code",code);
	request.setAttribute("codeType",codeType);
	request.setAttribute("date",date);
	request.setAttribute("supplier",supplier);
	
	pageContext.forward("/vm/procure/procureCancel.vm");
%>
<%!
private String getExecl(HttpServletRequest request,Map map) {
		///导出execl
		String sServerFileName="";
		String sLocalFileName = "";
		java.util.Date dt = new java.util.Date(System.currentTimeMillis());
		SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		sServerFileName= "exScarch"+fmt.format(dt);
		String sWebRootPath = request.getRealPath("/");//得到你的web应用的根。
		String sPath=sWebRootPath+"exportTemp/";
		System.out.println("targetfile==============================================================》"+sPath);
		request.setAttribute("xlsName",sServerFileName+".xls");
		String execlName = sPath+sServerFileName+".xls";
		//sServerFileName =sServerFileName + sExt;
		//如果不存在该目录，则新建一个
		//File dir =new File(sPath);
		File dir =new File(sPath);
		if (!dir.exists()){
			dir.mkdirs();
		}
		String targetfile = execlName;// 输出的excel文件名  
        String worksheet = "check_"+sServerFileName;// 输出的excel文件工作表名
         
        String[] title = { "行号","临时采购订单编号 ","项目编码","项目名称","申请人","推荐供应商","申请日期","转临日期"};// excel工作表的标题
       	WritableWorkbook workbook;
       
       	 try {  
            //System.out.println("begin");  
  
            OutputStream os = new FileOutputStream(targetfile);  
            workbook = Workbook.createWorkbook(os);  
            WritableSheet sheet = workbook.createSheet(worksheet, 0); // 添加第一个工作表   
            //设置内容格式  
            WritableCellFormat contentFormat = new WritableCellFormat();  
            contentFormat.setAlignment(Alignment.CENTRE);  
            Label label;  
            for (int i = 0; i < title.length; i++) {  
                // Label(列号,行号 ,内容 )  
                label = new jxl.write.Label(i, 0, title[i]); // put the title in  
                                                                // row1  
                label.setCellFormat(contentFormat);  
                sheet.addCell(label);  
            }
            if(map!=null){
            	List list  = (List)map.get("data");
            		//System.out.println("targetfile==============================================================》"+list.size());
            	if(list.size()>0){
            		for(int j=0;j<list.size();j++){
            			Object[] ex = (Object[])list.get(j);
            			//label = new jxl.write.Label(j, 0, ""+j);
            			//System.out.println("targetfile==============================================================》"+ex);
            			int line=0;
            			for(int k=0;k<ex.length;k++){
            				if(k==0){
            					label = new jxl.write.Label(k, j+1,j+1+"");
            					label.setCellFormat(contentFormat);  
                				sheet.addCell(label);
            				}else{
            						label = new jxl.write.Label(k+line, j+1,ex[k].toString());
            						label.setCellFormat(contentFormat);  
                					sheet.addCell(label);
                				
            				}
            				
            			}
            		}	
            	}
            }
            workbook.write();  
            workbook.close();  
        }catch (Exception e) {  
            e.printStackTrace();  
        }  
	return sServerFileName+".xls";
}
%>